<?php 
 require_once "../includes/config.php";
 require_once "../includes/pagination.php";


 $cond = '';
 $sort = '';
 $ut = $u = $fn = $ln = $dg = 'asc';
 if(isset($_GET['sort']))
 {
  if($_GET['sort']=='category_name')
  {
   if($_GET['s_type']=='asc')
   {	$cond=' ORDER BY category_name ASC';	$sort = '&sort=category_name&s_type=asc';	$ut='desc';	}
   else
   {	$cond=' ORDER BY category_name DESC';	$sort = '&sort=category_name&s_type=desc';	}
  }
 }
 
 
 $itemPerPage=15;
 if(isset($_REQUEST['subcatId']))
 {
 $subcatId=$_REQUEST['subcatId'];
 $sqlShwCat="SELECT * FROM tbl_category where parent_id=$subcatId";
 $rstShwCat=mysql_query($sqlShwCat);
 $rowShwCat=mysql_fetch_array($rstShwCat);
 $categoryShwName=$rowShwCat['category_name'];
 }
 
 if(!empty($_REQUEST['delCategory']))
 {
 	
	$categoryId=$_REQUEST['delCategory'];
 	$sqlDelCat="Delete from tbl_category where category_id='$categoryId'";
	$rstDelCat=mysql_query($sqlDelCat);
	if($rstDelCat) 
	{ 
	$msgDelScat="Sub-subcategory Deleted Successfully";	
	}
	
 }
 ?>
 <script type="text/javascript">
 function categorySubmit(val)
   { 
	 window.location="index.php?listsscat&cat="+val;
   }
   function subcategorySubmit(val,cat)
   { 
	 window.location="index.php?listsscat&cat="+cat+"&scat="+val;
   }
   
function addSubSubCat(cat, scat) {
	window.location="index.php?addsscat&cat="+cat+"&scat="+scat;
}

 function editSubategory(subcat)
 {
	window.location="index.php?editscat&esubcat="+subcat;
 }
//Delete Category
function deleteCategory(catId)
{
	var delCat=confirm("Are you sure want to delete Sub-subCategory?");
  if(delCat==true)
  {
	  var x=document.getElementById("formDelCategory");
	  document.getElementById("delCategory").value=catId;
	  x.submit();
  }
}
 </script>
 <form action="" method="post" name="formDelCategory" id="formDelCategory">
 	<input type="hidden" name="delCategory" id="delCategory">
<table width="100%"  border="1" cellspacing="0" class="container">
  <tr>
    <td width="469" height="25" class="tdHeader">Sub-SubCategory</td>
  </tr>  
  <tr>
    <td height="577">
		<table width="100%">
	    <tbody>
		<tr>
			<td>
				<form name="frmSearchPro" action="" method="post">
				<table width="100%" border="0">
					<tr>
						<td width="30%">Category Name</td>
						<td width="70%">
							<input type="text" name="category_name" id="category_name" />
						</td>
					</tr>
					

					<tr>
						<td height="35" colspan="5" style="text-align:center"><input type="submit" name="btnSearchPro" value="Search Category" /></td>
					</tr>
				</table>
				</form>
			</td>
		</tr>
		<tr>
			<td>
				<b>Sort  By :</b>
				<?
				if(!empty($_REQUEST['scat']))
				{
				 ?>
				 <a href="<?php echo $base_url;?>admin/index.php?listsscat&cat=<?php echo $_REQUEST['cat']?>&scat=<?php echo $_REQUEST['scat']?>&sort=category_name&s_type=<?php echo $ut;?>">Category Name</a> &nbsp;&nbsp;&nbsp;
				 <?php
				}
				else
				{
				?>
				 <a href="<?php echo $base_url;?>admin/index.php?listsscat&sort=category_name&s_type=<?php echo $ut;?>">Category Name</a> &nbsp;&nbsp;&nbsp;
				<?php
				}
				?>
				
			</td>
		</tr>
		</tbody>
	</table><br>
			<table align="center" width="50%" border="0" cellspacing="0"><tr>
			<td>Select Category</td>
				<td>
						<select name="cat_name" id="cat_name" onChange="javascript:categorySubmit(this.value)" class="fieldMax1">
				        <option value="">..Select Category..</option> 
			  		 <?php			   
					   $sqlSelCategory="SELECT * FROM tbl_category where parent_id=0";
					   $rstSelCategory=mysql_query($sqlSelCategory);
					   while($rowSelCategory=mysql_fetch_array($rstSelCategory))
					   {
						   $categoryId=$rowSelCategory['category_id'];
						   $categoryName=$rowSelCategory['category_name'];
						   
						    if(!empty($_REQUEST['cat'])) {
								if($categoryId==$_REQUEST['cat']) {
										$select="selected";
								} else {
										$select="";
								}
							} else { $select=""; }
						   
						   echo "<option value='$categoryId' $select>$categoryName</option>";
					   }
					 ?>
         				</select>
 			  </td>
			   </tr>
			   <tr>
		  <td>Select Subcategory </td>
		  <td>
				<select name="subcat_name" id="subcat_name" onChange="javascript:subcategorySubmit(this.value,<?php echo $_REQUEST['cat']; ?>)" class="fieldMax1">
				  <option>..Select Subcategory..</option> 
			   <?php	
			   $cId=$_REQUEST['cat'];		   
			   $sqlSelScat="SELECT * FROM tbl_category where parent_id=$cId";
			   $rstSelScat=mysql_query($sqlSelScat);
			   while($rowSelScat=mysql_fetch_array($rstSelScat))
			   {
				   $subcategoryId=$rowSelScat['category_id'];
				   $subcategoryName=$rowSelScat['category_name'];
				   if(!empty($_REQUEST['cat'])) {						
						if($subcategoryId==$_REQUEST['scat']) {
								$sel="selected";
						} else {
								$sel="";
						}
					} else { $sel=""; } 
					 
				   echo "<option value='$subcategoryId' $sel>$subcategoryName</option>";
			   }   
			 
			   ?>
               </select>
			</td>
	  </tr>
	 </table>
	 <br><br>
	<table width="100%"  border="0" cellspacing="0">
	 <tr>
	   <td height="27"  class="line">&nbsp;</td>
	   <td class="line">&nbsp;</td>	   
	   <td colspan="3"  class="line" style="text-align:right;">
	   		<input type='button' name='addsscat' value='Add Sub-subcategory' class='button' onclick="addSubSubCat('<?php echo $_GET['cat']; ?>', '<?php echo $_GET['scat']; ?>');">
		</td>
	 </tr>
	 <tr class="InnerHeader">
	   <td width="1%"  class="line">&nbsp;</td>
	   <td width="40%" class="line">Subcategory Name</td>
	   <td width="41%" class="line">Description</td>
	   <td width="9%" class="line"></td>
	   <td width="9%" class="line"></td>
	 </tr>	 
	 <?php
		 if(isset($_REQUEST['scat']))
		 {
		  
		    $search='';
		    if(isset($_POST['btnSearchPro'])) {
		     $category_name 	= $_POST['category_name'];		
		     if($category_name!='')
		      $search.= " AND category_name='".$category_name."'";
		    }
		    $subcatId=$_REQUEST['scat'];
		    $sqlShwSubcat="select * from tbl_category where  parent_id=$subcatId $search $cond";
		    $rstShwSubcat=mysql_query(getPagingQuery($sqlShwSubcat,$itemPerPage));
		    $numShwSubcat=mysql_num_rows($rstShwSubcat);
		    if($numShwSubcat==0) {
			   echo "<tr><td></td><td colspan='4'><h3>No Sub-subcategories Found...</h3></td></tr>";
		    }
		    else {
		    while($rowShwSubCat=mysql_fetch_array($rstShwSubcat)) {
			   $subcatId=$rowShwSubCat['category_id'];
			   $subcatName=$rowShwSubCat['category_name'];
			   $subcatDesc=$rowShwSubCat['category_description'];				
			   echo "<tr>";
			   echo "<td class='line'>&nbsp;</td>";
			   echo "<td class='line'>$subcatName</td>";
			   echo "<td class='line'>$subcatDesc</td>";
			   echo "<td class='line'><input type='button' name='edit' value='Edit' class='button' onclick='editSubategory($subcatId);'></td>";
			   echo "<td height='20' class='line'><input type='button' name='delete' value='Delete' class='button'  onclick='deleteCategory($subcatId);'></td> ";
			   echo "</tr>";
		    }
		  }
		 
		 ?>
	 <tr>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td>&nbsp;</td>
	  <td></td>
	  <td></td>
	  </tr>
	</table>
	<div align="center">
	 <?php
	 $link="listsscat&cat=$cId&scat=".$_REQUEST['scat'];
	 echo $disp=getPagingLink($sqlShwSubcat,$itemPerPage,$link);							   
	 }?>
	</div>
</td>
</tr>
</table>
</form>